package com.leyou.search.web;


import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-05-30 18:50
 **/
@RestController
@RequestMapping("/goods")
public class SearchController {

    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 初始化索引库
     */
    @GetMapping("/initialization")
    public Mono<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return Mono.just("导入成功");
    }
    /**
     *@描述   根据词条获取自动补全字段
     *@参数  [key]
     *@返回值  reactor.core.publisher.Mono<java.util.List<java.lang.String>>
     *@创建人  hhw
     *@创建时间  2020/5/31
     *@修改人和其它信息
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String key){
        return searchService.getSuggestion(key);
    }
    /**
     *@描述   查询商品展示页面的商品
     *@参数  [searchParamDTO]
     *@返回值  reactor.core.publisher.Mono<java.util.List<com.leyou.starter.elastic.entity.PageInfo<com.leyou.search.entity.Goods>>>
     *@创建人  hhw
     *@创建时间  2020/5/31
     *@修改人和其它信息
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO searchParamDTO){
        return searchService.searchGoods(searchParamDTO);
    }
    /**
     *@描述   根据查询条件聚合出过滤项
     *@参数  [searchParamDTO]
     *@返回值  reactor.core.publisher.Mono<java.util.Map<java.lang.String,java.util.List<?>>>
     *@创建人  hhw
     *@创建时间  2020/5/31
     *@修改人和其它信息
     */
    @PostMapping("/filter")
    public Mono<Map<String, List<?>>> getFilters(@RequestBody SearchParamDTO searchParamDTO) {

        return searchService.getFilters(searchParamDTO);
    }




}
